home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / os2 / octa209s.zip / octave-2.09 / src / time.cc < prev    next >
C/C++ Source or Header  |  1997-05-26  |  8KB  |  321 lines

  1. /*
  2.  
  3. Copyright (C) 1996 John W. Eaton
  4.  
  5. This file is part of Octave.
  6.  
  7. Octave is free software; you can redistribute it and/or modify it
  8. under the terms of the GNU General Public License as published by the
  9. Free Software Foundation; either version 2, or (at your option) any
  10. later version.
  11.  
  12. Octave is distributed in the hope that it will be useful, but WITHOUT
  13. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  15. for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with Octave; see the file COPYING.  If not, write to the Free
  19. Software Foundation, 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  20.  
  21. */
  22.  
  23. #ifdef HAVE_CONFIG_H
  24. #include <config.h>
  25. #endif
  26.  
  27. #include <string>
  28.  
  29. #include<iostream.h>
  30.  
  31. #include "defun-dld.h"
  32. #include "error.h"
  33. #include "help.h"
  34. #include "oct-map.h"
  35. #include "systime.h"
  36. #include "ov.h"
  37. #include "oct-obj.h"
  38. #include "utils.h"
  39.  
  40. // Date and time functions.
  41.  
  42. static Octave_map
  43. mk_tm_map (struct tm *tm, double fraction)
  44. {
  45.   Octave_map m;
  46.  
  47.   m ["usec"] = fraction * 1e6;
  48.   m ["sec"] = (double) tm->tm_sec;
  49.   m ["min"] = (double) tm->tm_min;
  50.   m ["hour"] = (double) tm->tm_hour;
  51.   m ["mday"] = (double) tm->tm_mday;
  52.   m ["mon"] = (double) tm->tm_mon;
  53.   m ["year"] = (double) tm->tm_year;
  54.   m ["wday"] = (double) tm->tm_wday;
  55.   m ["yday"] = (double) tm->tm_yday;
  56.   m ["isdst"] = (double) tm->tm_isdst;
  57.  
  58. #if defined (HAVE_TM_ZONE)
  59.   m ["zone"]  = tm->tm_zone;
  60. #elif defined (HAVE_TZNAME)
  61.   if (tm->tm_isdst == 0 || tm->tm_isdst == 1)
  62.     m ["zone"] = tzname[tm->tm_isdst];
  63. #endif
  64.  
  65.   return m;
  66. }
  67.  
  68. static struct tm*
  69. extract_tm (Octave_map &m, double& fraction)
  70. {
  71.   static struct tm tm;
  72.  
  73.   fraction = (m ["usec"] . double_value ()) / 1e6;
  74.   tm.tm_sec = NINT (m ["sec"] . double_value ());
  75.   tm.tm_min = NINT (m ["min"] . double_value ());
  76.   tm.tm_hour = NINT (m ["hour"] . double_value ());
  77.   tm.tm_mday = NINT (m ["mday"] . double_value ());
  78.   tm.tm_mon = NINT (m ["mon"] . double_value ());
  79.   tm.tm_year = NINT (m ["year"] . double_value ());
  80.   tm.tm_wday = NINT (m ["wday"] . double_value ());
  81.   tm.tm_yday = NINT (m ["yday"] . double_value ());
  82.   tm.tm_isdst = NINT (m ["isdst"] . double_value ());
  83.  
  84. #if defined (HAVE_TM_ZONE)
  85.   static char *tm_zone = 0;
  86.  
  87.   string tstr = m ["zone"] . string_value ();
  88.  
  89.   delete [] tm_zone;
  90.   tm_zone = strsave (tstr.c_str ());
  91.  
  92.   tm.tm_zone = tm_zone;
  93. #endif
  94.  
  95.   return &tm;
  96. }
  97.  
  98. DEFUN_DLD (time, , ,
  99.   "time ()\n\
  100. \n\
  101. Return current time.  On Unix systems, this is the number of\n\
  102. seconds since the epoch.")
  103. {
  104.   time_t now;
  105.   double fraction = 0.0;
  106.  
  107. #if defined (HAVE_GETTIMEOFDAY)
  108.  
  109.   struct timeval tp;
  110.  
  111. #if defined  (GETTIMEOFDAY_NO_TZ)
  112.   gettimeofday (&tp);
  113. #else
  114.   gettimeofday (&tp, 0);
  115. #endif
  116.  
  117.   now = tp.tv_sec;
  118.  
  119.   fraction = tp.tv_usec / 1e6;
  120.  
  121. #else
  122.  
  123.   now = time (0);
  124.  
  125. #endif
  126.  
  127.   return (double) now + fraction;
  128. }
  129.  
  130. DEFUN_DLD (gmtime, args, ,
  131.   "gmtime (TIME)\n\
  132. \n\
  133. Given a value returned from time(), return a structure like that\n\
  134. returned from localtime() but with values corresponding to\n\
  135. Coordinated Universal Time (UTC).")
  136. {
  137.   octave_value_list retval;
  138.  
  139.   if (args.length () == 1)
  140.     {
  141.       double tmp = args(0).double_value ();
  142.  
  143.       if (! error_state)
  144.     {
  145.       time_t timeval = NINT (tmp);
  146.       double ip;
  147.       double fraction = modf (tmp, &ip); 
  148.  
  149.       retval = octave_value (mk_tm_map (gmtime (&timeval), fraction));
  150.     }
  151.     }
  152.   else
  153.     print_usage ("gmtime");
  154.  
  155.   return retval;
  156. }
  157.  
  158. DEFUN_DLD (localtime, args, ,
  159.   "localtime (TIME)\n\
  160. \n\
  161. Given a value returned from time(), return a structure with\n\
  162. the following elements:\n\
  163. \n\
  164.   usec  : microseconds after the second (0, 999999)\n\
  165.   sec   : seconds after the minute (0, 61)\n\
  166.   min   : minutes after the hour (0, 59)\n\
  167.   hour  : hours since midnight (0, 23)\n\
  168.   mday  : day of the month (1, 31)\n\
  169.   mon   : months since January (0, 11)\n\
  170.   year  : years since 1900\n\
  171.   wday  : days since Sunday (0, 6)\n\
  172.   yday  : days since January 1 (0, 365)\n\
  173.   isdst : Daylight Savings Time flag\n\
  174.   zone  : Time zone")
  175. {
  176.   octave_value_list retval;
  177.  
  178.   if (args.length () == 1)
  179.     {
  180.       double tmp = args(0).double_value ();
  181.  
  182.       if (! error_state)
  183.     {
  184.       time_t timeval = NINT (tmp);
  185.       double ip;
  186.       double fraction = modf (tmp, &ip); 
  187.  
  188.       retval = octave_value (mk_tm_map (localtime (&timeval), fraction));
  189.     }
  190.     }
  191.   else
  192.     print_usage ("localtime");
  193.  
  194.   return retval;
  195. }
  196.  
  197. DEFUN_DLD (mktime, args, ,
  198.   "mktime (TMSTRUCT)")
  199. {
  200.   octave_value_list retval;
  201.  
  202.   if (args.length () == 1 && args(0).is_map ()) 
  203.     {
  204.       Octave_map map = args(0).map_value ();
  205.  
  206.       double fraction;
  207.  
  208.       struct tm *tm = extract_tm (map, fraction);
  209.  
  210.       if (! error_state)
  211.     retval = (double) mktime (tm) + fraction;
  212.     }
  213.   else
  214.     print_usage ("mktime");
  215.  
  216.   return retval;
  217. }
  218.  
  219. DEFUN_DLD (strftime, args, ,
  220.   "strftime (FMT, TMSTRUCT)\n\
  221. \n\
  222. Performs `%' substitutions similar to those in printf.  Except where\n\
  223. noted, substituted fields have a fixed size; numeric fields are\n\
  224. padded if necessary.  Padding is with zeros by default; for fields\n\
  225. that display a single number, padding can be changed or inhibited by\n\
  226. following the `%' with one of the modifiers described below.\n\
  227. Unknown field specifiers are copied as normal characters.  All other\n\
  228. characters are copied to the output without change.\n\
  229. \n\
  230. Supports a superset of the ANSI C field specifiers.\n\
  231. \n\
  232. Literal character fields:\n\
  233. \n\
  234.   %    %\n\
  235.   n    newline\n\
  236.   t    tab\n\
  237. \n\
  238. Numeric modifiers (a nonstandard extension):\n\
  239. \n\
  240.   -    do not pad the field\n\
  241.   _    pad the field with spaces\n\
  242. \n\
  243. Time fields:\n\
  244. \n\
  245.   %H  hour (00..23)\n\
  246.   %I  hour (01..12)\n\
  247.   %k  hour ( 0..23)\n\
  248.   %l  hour ( 1..12)\n\
  249.   %M  minute (00..59)\n\
  250.   %p  locale's AM or PM\n\
  251.   %r  time, 12-hour (hh:mm:ss [AP]M)\n\
  252.   %R  time, 24-hour (hh:mm)\n\
  253.   %s  time in seconds since 00:00:00, Jan 1, 1970 (a nonstandard extension)\n\
  254.   %S  second (00..61)\n\
  255.   %T  time, 24-hour (hh:mm:ss)\n\
  256.   %X  locale's time representation (%H:%M:%S)\n\
  257.   %Z  time zone (EDT), or nothing if no time zone is determinable\n\
  258.   %z  offset from GMT\n\
  259. \n\
  260. Date fields:\n\
  261. \n\
  262.   %a  locale's abbreviated weekday name (Sun..Sat)\n\
  263.   %A  locale's full weekday name, variable length (Sunday..Saturday)\n\
  264.   %b  locale's abbreviated month name (Jan..Dec)\n\
  265.   %B  locale's full month name, variable length (January..December)\n\
  266.   %c  locale's date and time (Sat Nov 04 12:02:33 EST 1989)\n\
  267.   %C  century (00..99)\n\
  268.   %d  day of month (01..31)\n\
  269.   %e  day of month ( 1..31)\n\
  270.   %D  date (mm/dd/yy)\n\
  271.   %h  same as %b\n\
  272.   %j  day of year (001..366)\n\
  273.   %m  month (01..12)\n\
  274.   %U  week number of year with Sunday as first day of week (00..53)\n\
  275.   %w  day of week (0..6)\n\
  276.   %W  week number of year with Monday as first day of week (00..53)\n\
  277.   %x  locale's date representation (mm/dd/yy)\n\
  278.   %y  last two digits of year (00..99)\n\
  279.   %Y  year (1970...)")
  280. {
  281.   octave_value_list retval;
  282.  
  283.   if (args.length () == 2 && args(0).is_string () && args(1).is_map ()) 
  284.     {
  285.       string fmt = args(0).string_value ();
  286.  
  287.       Octave_map map = args(1).map_value ();
  288.  
  289.       double fraction;
  290.  
  291.       struct tm *tm = extract_tm (map, fraction);
  292.  
  293.       if (! error_state)
  294.     {
  295.       const char *fmt_str = fmt.c_str ();
  296.  
  297.       size_t bufsize = strftime (0, (size_t) UINT_MAX, fmt_str, tm);
  298.  
  299.       char *buf = new char [++bufsize];
  300.  
  301.       buf[0] = '\0';
  302.  
  303.       strftime (buf, bufsize, fmt_str, tm);
  304.  
  305.       retval = buf;
  306.  
  307.       delete [] buf;
  308.     }
  309.     }
  310.   else
  311.     print_usage ("strftime");
  312.  
  313.   return retval;
  314. }
  315.  
  316. /*
  317. ;;; Local Variables: ***
  318. ;;; mode: C++ ***
  319. ;;; End: ***
  320. */
  321.